package com.essroid.study;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import com.essroid.study.db.DatabaseHelper;

import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class EasyAndroid extends Activity {
    /** Called when the activity is first created. */

	private EditText mEditName;
	private Button mButton;
	private ListView mView;

	private DatabaseHelper mDatabase;
	private SQLiteDatabase mdb;
	private SQLiteStatement mstmt;
	private Cursor mcur;

	private ArrayAdapter<CharSequence> mList;

	private String mInit = "1:登録してください。";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onStart() {
    	super.onStart();
        mEditName = (EditText)findViewById(R.id.editName);
        mButton = (Button)findViewById(R.id.button1);

        mEditName.setText(null);
        mEditName.setHint("何か入力して下さい。");
        mButton.setText("実行ボタン");

        mView = (ListView)findViewById(R.id.listView1);
        mButton.setOnClickListener(new ButtonController());

        mDatabase = new DatabaseHelper(this);
        mdb = mDatabase.getWritableDatabase();

        mList = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1);

        mcur = mdb.query("EasyAndroid", new String[]{"Date","Subject"}, null, null, null, null, "_id");
        mcur.moveToFirst();
        if(mcur.getCount()>0){
        	for(int i = 0;i < mcur.getCount();i++){
        		mList.add(mcur.getString(0) + "-" + mcur.getString(1));
        		mcur.moveToNext();
        	}
        }else{
            mList.add(mInit);
        }

        mView.setAdapter(mList);

        mView.setOnItemClickListener(new ViewController());

        Log.d("EasyAndroid", "onStart");

    }

    @Override
    protected void onStop() {

    	mcur.close();
    	mdb.close();
    	mDatabase.close();

    	mcur = null;
    	mdb = null;
    	mDatabase = null;

    	mEditName = null;
    	mButton = null;
    	mList = null;
    	mView = null;
    	super.onStop();
        Log.d("EasyAndroid", "onStop");
    }


    private String storeDB() throws SQLException{
    	String INSERT = "INSERT INTO EasyAndroid(date,subject) VALUES(?,?);";
    	String _date = "";

    	Calendar cal = Calendar.getInstance(Locale.JAPANESE);
    	SimpleDateFormat _df = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    	_date = _df.format(cal.getTime());

    	mdb.beginTransaction();
    	mstmt = mdb.compileStatement(INSERT);
    	mstmt.bindString(1, _date);
    	mstmt.bindString(2, mEditName.getText().toString());
    	mstmt.executeInsert();
    	mdb.setTransactionSuccessful();
    	mdb.endTransaction();

    	return _date + "-" + mEditName.getText().toString();
    }



    private class ButtonController implements OnClickListener{

		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.button1:
				String _newRow = storeDB();

				mList.add(_newRow);
				mView.invalidate();
				break;

			default:
				break;
			}
		}

    }

    private class ViewController implements OnItemClickListener{

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

			String _msg =  (String) ((TextView)arg1).getText();

			LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.layout.toast,(ViewGroup) findViewById(R.id.toast_layout_root));
			TextView text = (TextView) layout.findViewById(R.id.text);

			Toast toast = new Toast(arg1.getContext());
			text.setText(_msg);
			toast.setDuration(Toast.LENGTH_LONG);
			toast.setView(layout);
			toast.setGravity(Gravity.CENTER_VERTICAL, 0, 200);

			toast.show();

		}

    }

}